package com.xpsoft.oa.workflow.event;

import java.math.BigDecimal;
import java.util.Date;
import java.util.HashSet;
import java.util.Set;

import org.apache.commons.lang.StringUtils;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.jbpm.api.listener.EventListener;
import org.jbpm.api.listener.EventListenerExecution;
import org.jbpm.api.model.OpenProcessInstance;

import com.xpsoft.core.util.AppUtil;
import com.xpsoft.oa.model.flow.ProcessRun;
import com.xpsoft.oa.model.project.ProjectRegister;
import com.xpsoft.oa.model.system.AppUser;
import com.xpsoft.oa.model.system.FileAttach;
import com.xpsoft.oa.service.flow.ProcessRunService;
import com.xpsoft.oa.service.project.ProjectRegisterService;
import com.xpsoft.oa.service.system.AppUserService;
import com.xpsoft.oa.service.system.FileAttachService;

public class ProjectRegisterFinishListener implements EventListener{
	private Log logger=LogFactory.getLog(ProjectRegisterFinishListener.class);
	@Override
	public void notify(EventListenerExecution execution) throws Exception {
		//获取表单参数
		OpenProcessInstance pi=execution.getProcessInstance();
		String proName = (String)pi.getVariable("projectRegister.proName");
		String businesserUserId = (String)pi.getVariable("projectRegister.businesser.userId");
		String businessSource = (String)pi.getVariable("projectRegister.businessSource");
		String bidUnit = (String)pi.getVariable("projectRegister.bidUnit");
		String props = (String)pi.getVariable("projectRegister.props");
		String moneyProps = (String)pi.getVariable("projectRegister.moneyProps");
		String proArea = (String)pi.getVariable("projectRegister.proArea");
		BigDecimal preAmount = (BigDecimal)pi.getVariable("projectRegister.preAmount");
		Date preDuration = (Date)pi.getVariable("projectRegister.preDuration");
		Date preDurationEnd = (Date)pi.getVariable("projectRegister.preDurationEnd");
		String quality = (String)pi.getVariable("projectRegister.quality");
		String useArea = (String)pi.getVariable("projectRegister.useArea");
		String bidType = (String)pi.getVariable("projectRegister.bidType");
		String humanRelations = (String)pi.getVariable("projectRegister.humanRelations");
		String winBidProbability = (String)pi.getVariable("projectRegister.winBidProbability");
		String techBidMakerUserId = (String)pi.getVariable("projectRegister.techBidMaker.userId");
		String economicBidMakerUserId = (String)pi.getVariable("projectRegister.economicBidMaker.userId");
		Date endBidDate = (Date)pi.getVariable("projectRegister.endBidDate");
		BigDecimal bidDeposit = (BigDecimal)pi.getVariable("projectRegister.bidDeposit");
		Date registDate = (Date)pi.getVariable("projectRegister.registDate");
		String remark = (String)pi.getVariable("projectRegister.remark");
		String attachIds = (String)pi.getVariable("projectRegister.attachIds");

		ProjectRegister projectRegister = new ProjectRegister();
		projectRegister.setProName(proName);
		projectRegister.setBusinessSource(businessSource);
		projectRegister.setBidUnit(bidUnit);
		projectRegister.setProps(props);
		projectRegister.setMoneyProps(moneyProps);
		projectRegister.setProArea(proArea);
		projectRegister.setPreAmount(preAmount);
		projectRegister.setPreDuration(preDuration);
		projectRegister.setPreDurationEnd(preDurationEnd);
		projectRegister.setQuality(quality);
		projectRegister.setUseArea(useArea);
		projectRegister.setBidType(bidType);
		projectRegister.setHumanRelations(humanRelations);
		projectRegister.setWinBidProbability(winBidProbability);
		projectRegister.setEndBidDate(endBidDate);
		projectRegister.setRegistDate(registDate);
		projectRegister.setRemark(remark);
		projectRegister.setBidDeposit(bidDeposit);
		projectRegister.setStatus(0);//等待状态
		
		AppUserService appUserService = (AppUserService)AppUtil.getBean("appUserService");
		if(StringUtils.isNotBlank(businesserUserId)){
			projectRegister.setBusinesser(appUserService.get(Long.valueOf(businesserUserId)));
		}
		if(StringUtils.isNotBlank(techBidMakerUserId)){
			projectRegister.setTechBidMaker(appUserService.get(Long.valueOf(techBidMakerUserId)));
		}
		if(StringUtils.isNotBlank(economicBidMakerUserId)){
			projectRegister.setEconomicBidMaker(appUserService.get(Long.valueOf(economicBidMakerUserId)));
		}
		FileAttachService fileAttachService = (FileAttachService)AppUtil.getBean("fileAttachService");
		if(StringUtils.isNotBlank(attachIds)){
			Set<FileAttach> fileSet = new HashSet<FileAttach>();
			String fileIds[] = attachIds.split(",");
			projectRegister.getFileAttachs().clear();
			for(String fileId:fileIds){
				fileSet.add(fileAttachService.get(Long.valueOf(fileId)));
			}
			projectRegister.getFileAttachs().addAll(fileSet);
		}
		
		ProcessRunService processRunService=(ProcessRunService)AppUtil.getBean("processRunService");
		ProcessRun processRun=processRunService.getByExeId(pi.getId());
		projectRegister.setProcessRunId(processRun.getRunId());
		projectRegister.setProRegisterTitle(processRun.getSubject());
		
		ProjectRegisterService projectRegisterService = (ProjectRegisterService)AppUtil.getBean("projectRegisterService");
		projectRegisterService.save(projectRegister);
		
		
		//获取流程发起者对象
		AppUser flowStartUser=(AppUser)execution.getProcessInstance().getVariable("flowStartUser");
	
		//所有参与流程的都发消息
		AppUtil.sendSysShortMsg(execution.getProcessInstance().getId(), flowStartUser);
	}

}
